<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/appwap.php');

need_login(true);
$user = Table::Fetch('user',$login_user_id);
$mail = uencode($user['email']);
$fanli=array();
//获取一级邀请会员
$conditionf = array( 'user_id' => $login_user_id,'buy_time = 0',);
$fcount = Table::Count('invite', $conditionf);

$finvites = DB::LimitQuery('invite', array(
			'condition' => $conditionf,
			'order' => 'ORDER BY buy_time asc',
			));

$fuser_ids = Utility::GetColumn($finvites, 'other_user_id');
$fusers = Table::Fetch('user', $fuser_ids);
//获取各个用户返利
foreach($fuser_ids as $i=>$id){
	$conditionc=array('user_id'=>$id,'state'=>'pay',);
	$orders=DB::LimitQuery('order',array(
		'condition'=>$conditionc,
	));
	$order_ids=Utility::GetColumn($orders, 'id');
	$money=0;
	foreach($order_ids as $in=>$did){
		$condition_f=array('detail_id'=>$did,'action'=>'invite','user_id'=>$login_user_id,);
		$flow_f=DB::LimitQuery('flow',array('condition'=>$condition_f,));
		if($flow_f) $money+=$flow_f[0]['money'];
	}
	$fanli[$id]=$money;
}


//获取二级邀请会员
$scount=0;
$suser_ids=array();
foreach($fuser_ids as $index=>$id){
	$conditions= array( 'user_id' => $id,'buy_time = 0',);
	$count=Table::Count('invite', $conditions);
	$scount+=$count;
	if($count>0){
		$sinvites = DB::LimitQuery('invite', array(
			'condition' => $conditions,
			'order' => 'ORDER BY buy_time asc',
			));
		$suser_ids[]=Utility::GetColumn($sinvites, 'other_user_id');
	}
	
}

$susers = Table::Fetch('user', $suser_ids[0]);
//获取各个用户返利
foreach($suser_ids[0] as $i=>$id){
	$conditionc=array('user_id'=>$id,'state'=>'pay',);
	$orders=DB::LimitQuery('order',array(
		'condition'=>$conditionc,
	));
	$order_ids=Utility::GetColumn($orders, 'id');
	$money=0;
	foreach($order_ids as $in=>$did){
		$condition_f=array('detail_id'=>$did,'action'=>'invite','user_id'=>$login_user_id,);
		$flow_f=DB::LimitQuery('flow',array('condition'=>$condition_f,));
		if($flow_f) $money+=$flow_f[0]['money'];
	}
	$fanli[$id]=$money;
}


//获取三级邀请会员
$tcount=0;
$tuser_ids=array();
foreach($suser_ids as $index=>$id){
	$conditiont= array( 'user_id' => $id,'buy_time = 0',);
	$ccount=Table::Count('invite', $conditiont);
	$tcount+=$ccount;
	if($ccount>0){
	$tinvites = DB::LimitQuery('invite', array(
			'condition' => $conditiont,
			'order' => 'ORDER BY buy_time asc',
			));
	$tuser_ids[]=Utility::GetColumn($tinvites, 'other_user_id');
	}
}
$tusers = Table::Fetch('user', $tuser_ids[0]);

//获取各个用户返利
foreach($tuser_ids[0] as $i=>$id){
	$conditionc=array('user_id'=>$id,'state'=>'pay',);
	$orders=DB::LimitQuery('order',array(
		'condition'=>$conditionc,
	));
	$order_ids=Utility::GetColumn($orders, 'id');
	$money=0;
	foreach($order_ids as $in=>$did){
		$condition_f=array('detail_id'=>$did,'action'=>'invite','user_id'=>$login_user_id,);
		$flow_f=DB::LimitQuery('flow',array('condition'=>$condition_f,));
		if($flow_f) $money+=$flow_f[0]['money'];
	}
	$fanli[$id]=$money;
}


$total_count=$fcount+$scount+$tcount;

$condition = array( 
		'user_id' => $login_user_id, 
		'action' => 'invite',
		);
//$count = Table::Count('flow', $condition);
//list($pagesize, $offset, $pagestring) = pagestring($count, 20, true);

$flows = DB::LimitQuery('flow', array(
			'condition'=>$condition,
			//'size' => $pagesize,
			//'offset' => $offset,
			'order' => 'ORDER BY create_time DESC',
	));
$total_money=0;		
foreach($flows as $k=>$v){
	$total_money+=$v['money'];
}

die(include template('wap_myrefer'));
